package cc.rengu.igas.channel.upac.core.service.trans;


import cc.rengu.igas.channel.upac.common.constant.UpacTreeNodeConstant;
import cc.rengu.igas.channel.upac.common.enums.RespCodeEnum;
import cc.rengu.igas.channel.upac.common.enums.UpacProdEnum;
import cc.rengu.igas.channel.upac.core.model.ChnlBizResponse;
import cc.rengu.igas.channel.upac.core.service.base.GatewayPayService;
import cc.rengu.igas.channel.upac.facade.request.B2bConsumeRefundRequest;
import cc.rengu.igas.channel.upac.facade.response.B2bConsumeRefundResponse;
import cc.rengu.oltp.service.common.constant.TreeNodeConstant;
import cc.rengu.oltp.service.common.entity.DstChannelInfo;
import cc.rengu.oltp.service.common.enums.OltpRpcdEnum;
import cc.rengu.oltp.service.model.BizException;
import cc.rengu.oltp.utility.util.ConvertUtil;
import cc.rengu.oltp.utility.util.XmlTreeUtil;

/**
 * 企业网关消费退款服务 on 2019/10/8.
 */
public class B2BConsumeRefundService extends GatewayPayService {

    @Override
    protected Object beforeBizProcess() throws Exception {
        B2bConsumeRefundRequest b2bConsumeRefundRequest = new B2bConsumeRefundRequest();
        ConvertUtil.convertOutput(b2bConsumeRefundRequest);
        return b2bConsumeRefundRequest;
    }

    @Override
    protected ChnlBizResponse callBizService(Object request) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        int iReturnCode = 0;
        ChnlBizResponse chnlBizResponse = new ChnlBizResponse();
        B2bConsumeRefundResponse b2bConsumeRefundResponse = new B2bConsumeRefundResponse();
        B2bConsumeRefundRequest b2bConsumeRefundRequest = (B2bConsumeRefundRequest) request;
        DstChannelInfo dstChannelInfo = (DstChannelInfo) xmlTreeUtil.getXmlTreeObjectValue(UpacTreeNodeConstant.DST_CHANNEL_INFO, DstChannelInfo.class);
        ConvertUtil.convertInput(b2bConsumeRefundRequest);
        xmlTreeUtil.setXmlTreeStringValue(UpacTreeNodeConstant.txnType, "04");
        xmlTreeUtil.setXmlTreeStringValue(UpacTreeNodeConstant.txnSubType, "00");
        xmlTreeUtil.setXmlTreeStringValue(UpacTreeNodeConstant.bizType, UpacProdEnum.B2B.getProdCode());
        xmlTreeUtil.setXmlTreeStringValue(TreeNodeConstant.TXN_NUM, b2bConsumeRefundRequest.getHeader().getTxnNum());
        iReturnCode = schedule.callService(dstChannelInfo.getDstServiceName(), 0);
        if (98 == iReturnCode) {
            rglog.error("调用银联全渠道服务<{}>超时，iReturnCode:<{}>", dstChannelInfo.getDstServiceName(), iReturnCode);
            b2bConsumeRefundResponse.setRespCode(OltpRpcdEnum.BIZ_PROCESS_TIMEOUT.getRespCode());
            b2bConsumeRefundResponse.setRespDesc(OltpRpcdEnum.BIZ_PROCESS_TIMEOUT.getRespDesc());
            chnlBizResponse.setResult(b2bConsumeRefundResponse);
            return chnlBizResponse;
        } else if (0 != iReturnCode) {
            rglog.error("调用银联全渠道服务<{}>失败，iReturnCode:<{}>", dstChannelInfo.getDstServiceName(), iReturnCode);
            throw new BizException(RespCodeEnum.CALL_BIZ_SERVICE_ERROR.getRespCode(), RespCodeEnum.CALL_BIZ_SERVICE_ERROR.getRespDesc());
        }
        ConvertUtil.convertOutput(b2bConsumeRefundResponse);
        b2bConsumeRefundResponse.setRespCode(convertRespCode(b2bConsumeRefundResponse.getRespCode()));
        chnlBizResponse.setResult(b2bConsumeRefundResponse);
        return chnlBizResponse;
    }

    @Override
    protected void afterBizProcess(ChnlBizResponse response) throws Exception {
        B2bConsumeRefundResponse b2bConsumeRefundResponse = (B2bConsumeRefundResponse) response.getResult();
        ConvertUtil.convertInput(b2bConsumeRefundResponse);
    }


}
